var addPage = avalon.define({
	$id: "page",
	uid: "",
	level:'0',
	info:{},
	param:{},
	isScan:false,
	getUrl:function(){
		if(this.info.qrcodePath) {
			return DAHUA_CONFIG.REMOTE_URL + "/" + this.info.qrcodePath;
		}
		return '';
	},
	validate: {
		onValidateAll: function(reasons) {
			if(reasons.length) {
				// $.openLoadForm($.i18n.prop('contentRefresh'));
				mui.toast(reasons[0].getMessage());
			} else {
				submit();
			}
		}
	},
	formSubmit: function(e) {
		e.preventDefault();
		if(this.level=='1') {
			this.param.id=this.info.id;
			this.param.status=1;
			this.param.useStatus='2';
		}else if(this.level=='2') {
			this.param.id=this.info.id;
			this.param.useStatus='1';
		}
		this.validate.onManual();
	}
});

mui.ready(function() {
	var uid = $("#uid").val();
	addPage.uid = uid;

	if("" != uid) {
		mui.ajax('/user/get.json', {
			data: {
				'uid': uid
			},
			dataType: 'json',
			success: function(data) {
				if(""!=data&&null!=data){
					addPage.level=data.customer.level;
					i18nReady();
				}
			}
		});
	}
	var _id = $.getParameter("id");
	var _code = $.getParameter("code");
	var _param={};
	if(_id) {
		_param.id=_id;
	}else if(_code) {
		_param.code=_code;
		_param['dt.id']=uid;
		addPage.isScan=true;
	}
	mui.ajax('/customer/coupon/get.json', {
		data: _param,
		dataType: 'json',
		type:'post',
		success: function(data) {
			console.log(data);
			if('1'==data.status){
				addPage.info=data.object;

			}
		}
	});
});

function submit(){
	$.openLoadForm($.i18n.prop('contentRefresh'));
	var param={
		'id':addPage.param.id,
		'useStatus':addPage.param.useStatus
	};
	if(addPage.param.status) {
		param.status=addPage.param.status;
	}
	console.log(param);
	mui.ajax('/customer/coupon/use.json', {
		data: param,
		type:'post',
		dataType: 'json',
		success: function(data) {
			$.closeLoadForm();
			if(data.status==1) {
				mui.alert($.i18n.prop('User_Coupon_lbl18'), "", "ok", function(e) {
					var url = "../partner/index.html";
					mui.openWindow({
						url: url
					});
				});
			}else if(data.status==2){
				mui.alert($.i18n.prop('Scan_message10'));
			}
		}
	});
}
